﻿using System;
using System.Collections.Generic;
using System.Text;
using XPTable.Renderers;
using XPTable.Events;
using System.Windows.Forms;
using System.Drawing;

namespace HefaSyncTool.Controls
{

    /// <summary>
    /// A CellRenderer that draws Cell contents as strings
    /// </summary>
    public class HyperLinkRenderer : CellRenderer
    {
        private HyperLinnkColumn column;
        private float linkWidth = 0;
        private float sharpWidth = 5;
        private Rectangle submitRect;
        private Rectangle refreshRect;
        private Rectangle deleteRect;
        private Rectangle editRect;
        private float line = 1;
        private Point firstLineStart;
        private Point firstLineEnd;
        private Point secondLineStart;
        private Point secondLineEnd;

        private Point thirdLineStart;
        private Point thirdLineEnd;
        /// <summary>
        /// Initializes a new instance of the TextCellRenderer class with
        /// default settings
        /// </summary>
        public HyperLinkRenderer(HyperLinnkColumn col)
            : base()
        {
            column = col;
        }

        public void CalSize(Graphics g, bool showRefresh)
        {
            if (linkWidth <= 0)
            {
                try
                {
                    linkWidth = g.MeasureString("重发", base.Font).Width + 1;
                }
                catch
                {
                    linkWidth = 33.0498F;
                }
            }
            float width = 0;

            width = (linkWidth + 2 * sharpWidth) * 4 + 3 * line;


            float left = ClientRectangle.Left;

            if (ClientRectangle.Width < width)
            {
                width = ClientRectangle.Width;
            }
            else
            {
                left = (ClientRectangle.Width - width) / 2 + ClientRectangle.Left;
            }

            submitRect = new Rectangle((int)(left + sharpWidth), ClientRectangle.Top, (int)linkWidth, ClientRectangle.Height);
            refreshRect = new Rectangle((int)(left + sharpWidth * 3 + linkWidth + line), ClientRectangle.Top, (int)linkWidth, ClientRectangle.Height);
            deleteRect = new Rectangle((int)(left + sharpWidth * 5 + linkWidth * 2 + line), ClientRectangle.Top, (int)linkWidth, ClientRectangle.Height);
            editRect = new Rectangle((int)(left + sharpWidth * 7 + linkWidth * 3 + line), ClientRectangle.Top, (int)linkWidth, ClientRectangle.Height);

            firstLineStart = new Point((int)(left + sharpWidth * 2 + linkWidth), ClientRectangle.Top);
            firstLineEnd = new Point((int)(left + sharpWidth * 2 + linkWidth), ClientRectangle.Bottom);

            secondLineStart = new Point((int)(left + sharpWidth * 4 + linkWidth * 2 + line), ClientRectangle.Top);
            secondLineEnd = new Point((int)(left + sharpWidth * 4 + linkWidth * 2 + line), ClientRectangle.Bottom);


            thirdLineStart = new Point((int)(left + sharpWidth * 6 + linkWidth * 3 + line), ClientRectangle.Top);
            thirdLineEnd = new Point((int)(left + sharpWidth * 6 + linkWidth * 3 + line), ClientRectangle.Bottom);
        }




        public override void OnMouseUp(CellMouseEventArgs e)
        {
            base.OnMouseUp(e);
            Graphics g = Graphics.FromHwnd(column.Parent.Table.Handle);
            bool showRefresh = true;
            bool allowResend=true;
            if (e.Cell.Text == "1")
            {
                allowResend = false;
            }
            if (e.Cell.Data != null)
            {
                showRefresh = (bool)e.Cell.Data;
            }
            
            CalSize(g, showRefresh);
            if (allowResend && submitRect.Contains(e.X, e.Y))
            {
                column.RaiseEvent(e.Row, e.Column, 0);
            }

            if (showRefresh && this.refreshRect.Contains(e.X, e.Y))
            {
                column.RaiseEvent(e.Row, e.Column, 1);
            }
            if (this.deleteRect.Contains(e.X, e.Y))
            {
                column.RaiseEvent(e.Row, e.Column, 2);
            }

            if (this.editRect.Contains(e.X, e.Y))
            {
                column.RaiseEvent(e.Row, e.Column, 3);
            }
            g.Dispose();
        }


        public override void OnMouseMove(CellMouseEventArgs e)
        {
            base.OnMouseMove(e);
            
            Graphics g = Graphics.FromHwnd(column.Parent.Table.Handle);
            bool showRefresh = true;
            if (e.Cell.Data != null)
            {
                showRefresh = (bool)e.Cell.Data;
            }
            bool allowResend = true;
            if (e.Cell.Text == "1")
            {
                allowResend = false;
            }
            CalSize(g,showRefresh);
            if ((allowResend && submitRect.Contains(e.X, e.Y))
                || (showRefresh && this.refreshRect.Contains(e.X, e.Y))
                || this.deleteRect.Contains(e.X, e.Y)
                || this.editRect.Contains(e.X,e.Y)
                )
            {
                column.Parent.Table.Cursor = Cursors.Hand;
            }
            else
            {
                column.Parent.Table.Cursor = Cursors.Arrow;
            }
            g.Dispose();
        }

        public override void OnMouseLeave(CellMouseEventArgs e)
        {
            column.Parent.Table.Cursor = Cursors.Arrow;
            base.OnMouseLeave(e);
        }

        /// <summary>
        /// Raises the Paint event
        /// </summary>
        /// <param name="e">A PaintCellEventArgs that contains the event data</param>
        protected override void OnPaint(PaintCellEventArgs e)
        {
            base.OnPaint(e);
            
            bool showRefresh = true;
            if (e.Cell.Data != null)
            {
                showRefresh = (bool)e.Cell.Data;
            }
            // don't bother going any further if the Cell is null
            if (e.Cell == null)
            {
                return;
            }

            CalSize(e.Graphics,showRefresh);


            Pen pen = new Pen(Color.Black);

            if (e.Cell.Text == "1")
            {
                e.Graphics.DrawString("成功", base.Font, Brushes.Gray, submitRect, this.StringFormat);
            }
            else
            {
                e.Graphics.DrawString("重发", base.Font, base.ForeBrush, submitRect, this.StringFormat);
            }
            e.Graphics.DrawLine(pen, firstLineStart, firstLineEnd);

            if (showRefresh)
            {
                e.Graphics.DrawString("刷新", base.Font, base.ForeBrush, refreshRect, this.StringFormat);
            }
            else
            {
                e.Graphics.DrawString("刷新", base.Font, Brushes.Gray, refreshRect, this.StringFormat);
                
            }
            e.Graphics.DrawLine(pen, secondLineStart, secondLineEnd);
            
            e.Graphics.DrawString("删除", base.Font, base.ForeBrush, deleteRect, this.StringFormat);


            e.Graphics.DrawLine(pen, thirdLineStart, thirdLineEnd);

            e.Graphics.DrawString("编辑", base.Font, base.ForeBrush, editRect, this.StringFormat);

            // draw a focus rect around the cell if it is
            // enabled and has focus
            if (e.Focused && e.Enabled)
            {
                ControlPaint.DrawFocusRectangle(e.Graphics, base.ClientRectangle);
            }
        }
    }
}
